/**
 * @文件名称: BaseAction.java
 * @类路径:   com.dssrz.weibo4j.action
 * @描述:     the base action which is the superclass of all actions
 * @作者:     robin
 * @时间:     2011-7-25 下午01:58:14
 * @版本:     1.0.0
 */
package com.dssrz.weibo4j.action;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

/**
 * BaseAction
 * 
 * @类功能说明: The baseAction include all specific services and the baseService
 *         which used in actions, all actions extend the baseAction, and the
 *         baseAction include common properties, function etc. The baseService
 *         includes the baseDAO which do base database operations. @see (@link
 *         WEB-INF/applicationContext-action.xml#baseAction)
 * @类修改者: robin
 * @修改日期: 2011-9-4
 * @修改说明: change to an abstract action for extended
 * @作者: robin
 * @创建时间: 2011-7-25 下午01:58:14
 * @版本: 1.0.0
 */

public abstract class BaseAction extends ActionSupport implements
		ServletRequestAware, ServletResponseAware {

	private static final long serialVersionUID = 3763935570303059552L;

	protected Logger log = Logger.getLogger(BaseAction.class);
	protected HttpServletResponse response = null;
	protected HttpServletRequest request = null;
	protected HttpSession session = null;
	private String jsp;

	// common properties
	public int gotoPage = -1;
	public String keyword;
	public Long id;

	/**
	 * @return the gotoPage
	 */
	public int getGotoPage() {
		return gotoPage;
	}

	/**
	 * @param gotoPage
	 *            the gotoPage to set
	 */
	public void setGotoPage(int gotoPage) {
		this.gotoPage = gotoPage;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getPage() {
		return jsp;
	}

	public JSONArray listToJSON(List<Object> list) {
		JSONArray jsonarr = new JSONArray();
		Object qBean;
		int length = list.size();
		for (int i = 0; i < length; i++) {
			qBean = list.get(i);
			try {
				String _temp = JSONSerializer.toJSON(qBean).toString();
				jsonarr.add(JSONObject.fromObject(_temp));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return jsonarr;
	}

	protected ServletRequest getServletRequest() {
		return ServletActionContext.getRequest();
	}

	public void setServletRequest(HttpServletRequest req) {
		this.request = req;
		this.session = this.request.getSession();
	}

	protected ServletResponse getServletResponse() {
		return ServletActionContext.getResponse();
	}

	public void setServletResponse(HttpServletResponse rep) {
		this.response = rep;
		this.response.setCharacterEncoding("utf-8");
	}

	public String getJsp() {
		return jsp;
	}

	public void setJsp(String jsp) {
		this.jsp = jsp;
	}

	protected Map<String, Object> getParams() {
		return ServletActionContext.getContext().getParameters();
	}

}
